"""
Version: 0.1
Author: CarpeDiem
Date: 2022/7/15
Description: 贪心算法
背包问题:
贪婪法：在对问题求解时，总是做出在当前看来是最好的选择，不追求最优解，快速找到满意解。
输入：
20 6
电脑 200 20
收音机 20 4
钟 175 10
花瓶 50 2
书 10 1
油画 90 9
"""


class Thing(object):
    """物品"""
    def __init__(self, name, price, weight):
        self.name = name
        self.price = price
        self.weight = weight

    @property
    def value(self):
        """性价比"""
        return self.price / self.weight


def input_thing():
    name, price, weight = input().split()
    return name, int(price), int(weight)


def main():
    max_weight, num_of_thing = map(int, input().split())
    all_things = []
    for _ in range(num_of_thing):
        all_things.append(Thing(*input_thing()))
    total_weight = 0
    total_price = 0
    all_things.sort(key=lambda item: item.value, reverse=True)

    for thing in all_things:
        if total_weight + thing.weight <= max_weight:
            print(f'小偷拿走{thing.name}')
            total_price += thing.price
            total_weight += thing.weight
    print(f'总价值：{total_price}美元')


main()